#include"Mystdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#define FILE_MOD 0666

_FILE* _fopen(const char* file,const char mod){
    int flag = 0; 
    if(mod=='w') flag = O_WRONLY | O_CREAT | O_TRUNC; 
    else if(mod=='a') flag = O_WRONLY | O_CREAT | O_APPEND;
    else if(mod=='r') flag = O_RDONLY;
    else return NULL;

    int ret_fd = open(file,flag,FILE_MOD);
    if(ret_fd<0) return NULL;
    _FILE* fp = (_FILE*)malloc(sizeof(_FILE));
    assert(fp!=NULL);
    fp->fd = ret_fd;
    return fp;
}

int _fwrite(const char* buff,const int sz,const int num,const _FILE* fp){
    int ret = write(fp->fd,buff,sz*num);
    return ret;
}

int _fread(char* buff,const int sz,const int num,const _FILE* fp){
    int ret = read(fp->fd,buff,sz*num);
    return ret;
}

void _close(_FILE* fp){
    close(fp->fd);
    free(fp);
}
